package net.zhuike.novel.app.ui.fragment;


import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;

import com.umeng.analytics.MobclickAgent;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.CategoryList;
import net.zhuike.novel.app.bean.RankingList;
import net.zhuike.novel.app.bean.TabModel;
import net.zhuike.novel.app.bean.TabModelResult;
import net.zhuike.novel.app.common.OnRvItemClickListener;
import net.zhuike.novel.app.service.BookStoreService;
import net.zhuike.novel.app.ui.activity.SubRankActivity;
import net.zhuike.novel.app.ui.adapter.TopCategoryListAdapter;
import net.zhuike.novel.app.ui.adapter.TopRankAdapter;
import net.zhuike.novel.app.ui.base.BaseFragment;
import net.zhuike.novel.app.ui.base.Constant;
import net.zhuike.novel.app.ui.utils.SharedPreferencesUtil;
import net.zhuike.novel.app.ui.view.SupportGridItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import framework.common.views.InnerGridView;
import framework.common.zanetwork.core.ResponseCallback;
import okhttp3.Response;

/**
 * Created by wq on 2017/6/10.
 * 分类女频
 */
public class ClassificationFeMaleFragment extends BaseFragment {
    @Bind(R.id.rvFemaleCategory)
    InnerGridView mRvFeMaleCategory;
    private Context context;
    private int select = 0;
    private TopCategoryListAdapter mFemaleCategoryListAdapter;
    private List<TabModel> mFemaleCategoryList = new ArrayList<>();
    private int  showType=0;
    List<TabModel>tabList=new ArrayList<>();
    public static ClassificationFeMaleFragment getInstance(int  showType) {
        ClassificationFeMaleFragment sf = new ClassificationFeMaleFragment();
        sf.showType = showType;
        return sf;
    }
    @Override
    public int getLayoutResId() {
        return R.layout.fragment_store_classification_female;
    }

    @Override
    public void initDatas() {

    }

    @Override
    public void configViews() {
        context = getActivity();
        //分类
//        mRvFeMaleCategory.setHasFixedSize(true);
//        mRvFeMaleCategory.setLayoutManager(new GridLayoutManager(getActivity(), 3));
//        mRvFeMaleCategory.addItemDecoration(new SupportGridItemDecoration(getActivity()));
        mRvFeMaleCategory.setOnItemClickListener(clickItem);
        getCategoryList();
    }
    public void getCategoryList() {
        String topCategoryId= SharedPreferencesUtil.getInstance().getString(Constant.TopCategoryId);
        BookStoreService.getTab(context, topCategoryId, new ResponseCallback<TabModelResult>() {
            @Override
            public void onSuccess(TabModelResult advertisesModels) {
                if(advertisesModels!=null){
                    tabList.clear();
                    for(int i=0;i<advertisesModels.getSubCategories().size();i++){
                        TabModel model=advertisesModels.getSubCategories().get(i);
                        if(model.getId().equals("12")){
                            if(model.getSubCategories()!=null);
                            for (int j = 0; j <model.getSubCategories().size(); j++) {
                                TabModel models=model.getSubCategories().get(j);
                                if(models.getId().equals("107")) {
                                    if(models.getSubCategories()!=null) {
                                        tabList.addAll(models.getSubCategories());
                                    }
                                }
                            }

                        }
                    }
                }
                showCategoryList(tabList);
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }

    public void showCategoryList(List<TabModel>list) {

        mFemaleCategoryList.clear();
        mFemaleCategoryList.addAll(list);
        if(mFemaleCategoryListAdapter==null){
             mFemaleCategoryListAdapter = new TopCategoryListAdapter(mContext, mFemaleCategoryList, new ClickListeners(Constant.Gender.FEMALE));
             mRvFeMaleCategory.setAdapter(mFemaleCategoryListAdapter);
        }else {
            mFemaleCategoryListAdapter.data = mFemaleCategoryList;
            mFemaleCategoryListAdapter.notifyDataSetChanged();
        }
    }
    class ClickListeners implements OnRvItemClickListener<TabModel> {

        private String gender;

        public ClickListeners(@Constant.Gender String gender) {
            this.gender = gender;
        }

        @Override
        public void onItemClick(View view, int position, TabModel data) {
            SubRankActivity.startActivity(mContext,data.getName(),data.getId());
        }
    }
    AdapterView.OnItemClickListener clickItem = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            // TODO Auto-generated method stub

            TabModel data=mFemaleCategoryList.get(arg2);
            SubRankActivity.startActivity(mContext,data.getId(),data.getName());
        }

    };
    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("分类女频");
        MobclickAgent.onResume(context);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("分类女频");
        MobclickAgent.onPause(context);
    }
}
